package org.firebug.spring.boot.jackson.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * 金额格式化(4位分隔，保留2位，四舍五入)：12345.6789 -> 1,2345.68
 * @Author: 张晓东
 */
public class AmountToYuan42Serializer extends JsonSerializer<BigDecimal> {
    private static final String PATTERN = ",####.##";
    @Override
    public void serialize(BigDecimal amount, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        String value = new DecimalFormat(PATTERN).format(amount);
        gen.writeString(value);
    }
}
